//--------------------------------------------------------------
//
// Qizhi Zhang 
// ezhung@gmail.com
// http://www.moorwind.com
//
//--------------------------------------------------------------
package reader.services
{
	import com.moorwind.metaweblog.MetaWeblog;
	import com.moorwind.metaweblog.event.MetaWeblogEvent;
	import com.moorwind.metaweblog.vo.Post;
	
	import mx.core.UIComponent;
	import mx.managers.PopUpManager;
	
	import reader.ApplicationFacade;
	import reader.events.NamePasswordEvent;
	import reader.model.IDraftProxy;
	import reader.model.IServiceProxy;
	import reader.model.vo.BlogDraftVO;
	import reader.model.vo.ServiceVO;
	import reader.utils.FeedLoader;
	import reader.view.components.NeedUserLogin;

	public class MetaWeblogBaseService extends AbstractService 
										implements IPostBlogAble, IUpdateBlogAble
	{
		////////////////////////////////////////////////////////////////////////////////
		//protected variables
		
		protected var metaWeblog:MetaWeblog;
		
		////////////////////////////////////////////////////////////////////////////////
		//private variables
		
		private var _isPostBlogAble:Boolean;
		private var _isUpdateBlogAble:Boolean;
		
		private var _draftProxy:IDraftProxy;
		
		//////////////////////////////////////////////////////////////////////////////
		//constructor
		public function MetaWeblogBaseService(service:ServiceVO, proxy:IServiceProxy, parent:UIComponent=null)
		{
			super(service, proxy, parent);
			this.initialize();
		}
		
		////////////////////////////////////////////////////////////////////////////////
		//methods
		
		//----------------------------------------------------------------------------
		//
		// service initialize
		//
		//----------------------------------------------------------------------------
		
		override protected function initialize():void
		{
			if(!service.username || service.username == "" || !service.password || service.password == "")
			{
				if(loginInitCount < 5)
				{
					requestUserInfo(initialize);
					loginInitCount++;
				}
				else return;
			}
			metaWeblog = new MetaWeblog(service.endpoint, service.destination);
			metaWeblog.addEventListener(MetaWeblogEvent.RPC_ERROR, onPostFault_handler, false, 0, true);
		}
		private var loginInitCount:uint = 0;
		private var initCallBack:Function;
		private function requestUserInfo(callback:Function = null):void
		{
			var window:NeedUserLogin = PopUpManager.createPopUp(parent, NeedUserLogin, true) as NeedUserLogin;
			window.addEventListener(NamePasswordEvent.NAME_PSD_GET, onGetUserInfo_handler);
			initCallBack = callback;
		}
		private function onGetUserInfo_handler(e:NamePasswordEvent):void
		{
			service.username = e.username;
			service.password = e.password;
			if(e.isRemember)
			{
				proxy.updateService(service);
			}
			if(initCallBack!= null) initCallBack();
			initCallBack = null;
		}
		
		//----------------------------------------------------------------------------
		//
		// post blog
		//
		//----------------------------------------------------------------------------
		
		public function get isPostBlogAble():Boolean
		{
			return _isPostBlogAble;
		}
		
		public function set isPostBlogAble(value:Boolean):void
		{
			_isPostBlogAble = value;
		}
		
		public function get iDraftProxy():IDraftProxy
		{
			return _draftProxy;
		}
		
		public function set iDraftProxy(proxy:IDraftProxy):void
		{
			_draftProxy = proxy;
		}
		
		private var draft:BlogDraftVO;
		public function newBlog(blog:BlogDraftVO):void
		{
			draft = blog;
			var post:Post = new Post(blog.title, blog.description);
			metaWeblog.addEventListener(MetaWeblogEvent.NEW_POST, onNewPost_handler);
			metaWeblog.newPost(service.authparams, service.username, service.password, post, true);
			
		}
		private function onNewPost_handler(e:MetaWeblogEvent):void
		{
			metaWeblog.removeEventListener(MetaWeblogEvent.NEW_POST, onNewPost_handler);
			metaWeblog.addEventListener(MetaWeblogEvent.GET_POST, onGetNewPost_handler);
			metaWeblog.getPost(e.data.toString(), service.username, service.password);
		}
		private function onGetNewPost_handler(e:MetaWeblogEvent):void
		{
			metaWeblog.removeEventListener(MetaWeblogEvent.GET_POST, onGetNewPost_handler);
			var post:Post = e.data as Post;
			draft.postid = post.postid;
			draft.creatDate = post.dateCreated;
			draft.updateDate = post.dateCreated;
			draft.pubDate = post.dateCreated;
			if(!post.link || post.link == "")
			{
				if(service.config != "")
				{
					draft.link = FeedLoader.GetFeedsURI(service.config, draft, service);
				}
				else
				{
					draft.link = service.link;
				}				
			}
			else
			{
				draft.link = post.link;
			}
			
			draft.isPubed = true;
			draft.platformid = service.sid;
			draft.platforms = service.name;
			draft.deleteAble = false;
			_draftProxy.publishDraft(draft);
			proxy.sendNotification(ApplicationFacade.DRAFT_PUBLISHED, draft, service.name);
			draft = null;
		}
		
		//----------------------------------------------------------------------------
		//
		// update blog
		//
		//----------------------------------------------------------------------------
		
		public function get isUpdateBlogAble():Boolean
		{
			return _isUpdateBlogAble;
		}
		
		public function set isUpdateBlogAble(value:Boolean):void
		{
			_isUpdateBlogAble = value;
		}
		
		public function updateBlog(blog:BlogDraftVO):void
		{
			draft = blog;
			metaWeblog.addEventListener(MetaWeblogEvent.GET_POST, onGetUpdatePost_handler);
			metaWeblog.getPost(blog.postid, service.username, service.password);
		}
		private function onGetUpdatePost_handler(e:MetaWeblogEvent):void
		{
			metaWeblog.removeEventListener(MetaWeblogEvent.GET_POST, onGetUpdatePost_handler);
			var post:Post = e.data as Post;
			post.title = draft.title;
			post.description = draft.description;
			draft.pubDate = draft.updateDate = new Date();
			metaWeblog.addEventListener(MetaWeblogEvent.EDIT_POST, onEditPost_handler);
		}
		private function onEditPost_handler(e:MetaWeblogEvent):void
		{
			if(e.data)
			{
				_draftProxy.updatePublishedDraft(draft);
				proxy.sendNotification(ApplicationFacade.DRAFT_UPDATE_PUBED, draft, service.name);	
			}
			draft = null;
		}
		
		private function onPostFault_handler(e:MetaWeblogEvent):void
		{
			proxy.sendNotification(ApplicationFacade.SERVICE_ERROR, null, service.name);
		}
	}
}